#pragma once

#include <msg_node.h>
#include <QWidget>
#include <QString>
#include <QHostAddress>
#include <QHostInfo>
#include <QTcpSocket>
#include <QMessageBox>

class Session  : public QObject
{
	Q_OBJECT

public:
	Session(QObject *parent);

	void init();

	QTcpSocket::SocketState state();
	
	bool connectToHost(const QString& ip, uint16_t port);

	void disconnectFromHost();

	/// <summary>
	/// 读取最大maxLen个字节到buffer中
	/// </summary>
	/// <param name="buffer"></param>
	/// <param name="maxLen"></param>
	/// <returns></returns>
	size_t read(char* buffer, size_t maxLen);

	/// <summary>
	/// 写入buffer中的len个字节
	/// </summary>
	/// <param name="buffer"></param>
	/// <param name="len"></param>
	/// <returns></returns>
	bool write(char* buffer, size_t len);

	bool write(std::shared_ptr<SendNode> sendNode);

	bool isReadable();

	bool isConnected();

	~Session();
signals:
	void readyRead();
	void sigUpdateRecvData(const QString& data);
	void channelReadyRead(int channel);
	void bytesWritten(qint64 bytes);
	void channelBytesWritten(int channel, qint64 bytes);
	void aboutToClose();
	void readChannelFinished();

private:
	QString ip1_;
	uint16_t port1_;
	QString ip2_;
	uint16_t port2_;
	QString proto_;
	QTcpSocket socket_;
	QTcpSocket::SocketState state_;
};
